<?php

/**
 * This is the model class for table "caderno_instancia".
 *
 * The followings are the available columns in table 'caderno_instancia':
 * @property string $id
 * @property integer $id_caderno
 * @property string $data_criacao
 *
 * The followings are the available model relations:
 * @property Caderno $idCaderno
 */
class CadernoInstancia extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return CadernoInstancia the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'caderno_instancia';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_caderno, data_criacao', 'required'),
			array('id_caderno', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, id_caderno, data_criacao', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'caderno' => array(self::BELONGS_TO, 'Caderno', 'id_caderno'),
			'questoesInstancias' => array(self::HAS_MANY, 'CadernoInstanciaQuestao', 'id_caderno_instancia'),
			'questoes' => array(self::MANY_MANY,'Questao', 'caderno_instancia_questao(id_caderno_instancia,id_questao)'),
			
			
			
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_caderno' => 'Id Caderno',
			'data_criacao' => 'Data Criacao',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('id_caderno',$this->id_caderno);
		$criteria->compare('data_criacao',$this->data_criacao,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function getPercentualAcerto(){
		$quantidade = sizeof($this->questoes());
		$corretas=0;
		foreach($this->questoesInstancias() as $questaoInstancia){
			if($questaoInstancia->correta()){
				$corretas++;
			}
		}
		return round(($corretas/$quantidade)*100);
	}
	
	public function getPercentualAcertoDisciplina($idAssunto){
		
		$corretas = 0;

		$criteria = new CDbCriteria;
		$criteria->condition = 'id_caderno_instancia='.$this->id.' and q.id_assunto='.$idAssunto;
		$criteria->join = 'LEFT JOIN questao AS q ON t.id_questao = q.id'; 
		
		$questoesInstancias = CadernoInstanciaQuestao::model()->findAll($criteria);
		$quantidade = sizeof($questoesInstancias);
		foreach($questoesInstancias as $questaoInstancia){
				if($questaoInstancia->correta()){
					$corretas++;
				}
		}
		if($quantidade > 0){
			return round(($corretas/$quantidade)*100);
		} else {
			return 0;
		}
	}
	
}